package org.example.device.control;

import org.example.device.protocol.DeviceProtocol;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 智能摄像头控制器
 * 实现智能摄像头的控制逻辑
 */
@Component
public class SmartCameraController extends AbstractDeviceController {
    
    private static final String DEVICE_TYPE = "SMART_CAMERA";
    private static final List<String> SUPPORTED_COMMANDS = Arrays.asList(
        "switch", "record", "snapshot", "pan", "tilt", "zoom", "night_vision",
        "resolution", "patrol_start", "patrol_stop"
    );
    
    @Override
    public Map<String, Object> getDeviceProperties(Long deviceId) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("deviceType", DEVICE_TYPE);
        properties.put("supportedCommands", SUPPORTED_COMMANDS);
        properties.put("maxZoom", 10);
        properties.put("minZoom", 1);
        properties.put("supportedResolutions", Arrays.asList("720p", "1080p", "4K"));
        properties.put("hasNightVision", true);
        properties.put("hasPanTilt", true);
        return properties;
    }
    
    @Override
    public DeviceControlResult setDeviceProperties(Long deviceId, Map<String, Object> properties) {
        try {
            // 验证摄像头属性
            if (!validateCameraProperties(properties)) {
                return DeviceControlResult.failure("摄像头属性验证失败", "INVALID_PROPERTIES");
            }
            
            // 构建控制命令
            DeviceControlCommand command = new DeviceControlCommand(deviceId, "properties", properties);
            return executeCommand(command);
        } catch (Exception e) {
            return DeviceControlResult.failure("设置摄像头属性失败: " + e.getMessage());
        }
    }
    
    @Override
    public List<String> getSupportedCommands(Long deviceId) {
        return new ArrayList<>(SUPPORTED_COMMANDS);
    }
    
    @Override
    public String getDeviceType() {
        return DEVICE_TYPE;
    }
    
    /**
     * 智能摄像头使用HTTP协议
     */
    @Override
    protected DeviceProtocol getDeviceProtocol() {
        return protocolRegistry.getProtocol("HTTP");
    }
    
    /**
     * 验证摄像头属性
     */
    private boolean validateCameraProperties(Map<String, Object> properties) {
        if (properties == null) return false;
        
        // 验证分辨率
        if (properties.containsKey("resolution")) {
            Object resolution = properties.get("resolution");
            if (!(resolution instanceof String)) {
                return false;
            }
            List<String> validResolutions = Arrays.asList("720p", "1080p", "4K");
            if (!validResolutions.contains(resolution)) {
                return false;
            }
        }
        
        // 验证缩放级别
        if (properties.containsKey("zoom")) {
            Object zoom = properties.get("zoom");
            if (zoom instanceof Number) {
                int value = ((Number) zoom).intValue();
                if (value < 1 || value > 10) {
                    return false;
                }
            }
        }
        
        return true;
    }
    
    /**
     * 控制摄像头开关
     */
    public DeviceControlResult controlSwitch(Long deviceId, boolean on, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "switch", on);
        command.setUserId(userId);
        command.setAction("摄像头开关控制");
        return executeCommand(command);
    }
    
    /**
     * 开始/停止录制
     */
    public DeviceControlResult controlRecord(Long deviceId, boolean record, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "record", record);
        command.setUserId(userId);
        command.setAction("录制控制");
        return executeCommand(command);
    }
    
    /**
     * 拍摄快照
     */
    public DeviceControlResult takeSnapshot(Long deviceId, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "snapshot", null);
        command.setUserId(userId);
        command.setAction("拍摄快照");
        return executeCommand(command);
    }
    
    /**
     * 控制云台
     */
    public DeviceControlResult controlPanTilt(Long deviceId, String direction, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "pan_tilt", direction);
        command.setUserId(userId);
        command.setAction("云台控制");
        return executeCommand(command);
    }

    /**
     * 控制夜视模式
     */
    public DeviceControlResult controlNightVision(Long deviceId, boolean enabled, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "night_vision", enabled);
        command.setUserId(userId);
        command.setAction("夜视模式控制");
        return executeCommand(command);
    }

    /**
     * 设置分辨率
     */
    public DeviceControlResult setResolution(Long deviceId, String resolution, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "resolution", resolution);
        command.setUserId(userId);
        command.setAction("分辨率设置");
        return executeCommand(command);
    }

    /**
     * 控制水平角度
     */
    public DeviceControlResult controlPan(Long deviceId, Integer angle, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "pan", angle);
        command.setUserId(userId);
        command.setAction("水平角度调节");
        return executeCommand(command);
    }

    /**
     * 控制垂直角度
     */
    public DeviceControlResult controlTilt(Long deviceId, Integer angle, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "tilt", angle);
        command.setUserId(userId);
        command.setAction("垂直角度调节");
        return executeCommand(command);
    }

    /**
     * 开始巡航
     */
    public DeviceControlResult startPatrol(Long deviceId, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "patrol_start", null);
        command.setUserId(userId);
        command.setAction("开始巡航");
        return executeCommand(command);
    }

    /**
     * 停止巡航
     */
    public DeviceControlResult stopPatrol(Long deviceId, Long userId) {
        DeviceControlCommand command = new DeviceControlCommand(deviceId, "patrol_stop", null);
        command.setUserId(userId);
        command.setAction("停止巡航");
        return executeCommand(command);
    }
}
